package chenji.yourticket.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import chenji.yourticket.Main;
import chenji.yourticket.datacenter.PlayerData;
import chenji.yourticket.model.ModelInfo;

public class CommandYT implements CommandExecutor {
	@Override
	public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
		if (!(arg0 instanceof Player)) {
			arg0.sendMessage(Main.getMessageManager().getPlayer_Only());
			return true;
		}
		Player p = (Player) arg0;
		if (arg3.length == 0) {
			p.sendMessage("§c--------------------§a门票插件帮助§c---------------------");
			p.sendMessage("§c/yt openmodel §a-打开模板管理器");
			p.sendMessage("§c/yt gi §a-获取介绍。");
			p.sendMessage("§c/yt create §a-创建一个新的模板。");
			p.sendMessage("§c/yt edit [模板名称] §a-编辑一个现有的模板");
			p.sendMessage("§c/yt remove [模板名称] §a-删除一个现有的模板");
			p.sendMessage("§c/yt press [模板名称] §a-印刷一个现有的模板");
			p.sendMessage("§c-------------------------------------------------------");
			return true;
		}
	
		if (arg3[0].equalsIgnoreCase("gi")) {
			p.sendMessage(Main.getMessageManager().getCommand_Help().replaceAll("%player%", p.getName()));
			return true;
		}
		if (arg3[0].equalsIgnoreCase("openmodel")) {
			Main.getModelManager().openInventory(p);
			return true;
		}
		if (arg3[0].equalsIgnoreCase("create")) {
			Main.getEditorManager().newModelInfo(p);
			return true;
		}
		if (arg3[0].equalsIgnoreCase("edit")) {
			if (!(arg3.length == 2)) {
				p.sendMessage("§c/yt edit [模板名称] §a-编辑一个现有的模板");
			}
			ModelInfo m = this.getModel(arg3[1], p);
			if (m == null) {
				p.sendMessage(Main.getMessageManager().getMessage_UnknownModel());
				return true;
			}
			Main.getEditorManager().edit(p, m);
			return true;
		}
		if (arg3[0].equalsIgnoreCase("remove")) {
			if (!(arg3.length == 2)) {
				p.sendMessage("§c/yt remove [模板名称] §a-删除一个现有的模板");
			}
			PlayerData pd = Main.getPlayerDataChenter().getPlayerData(p);
			if (pd == null) {
				p.sendMessage(Main.getMessageManager().getMessage_UnkonwException());
				return true;
			}
			pd.removeModel(arg3[1]);
			p.sendMessage(Main.getMessageManager().getMessage_RemoveDone());
			return true;
		}
		if (arg3[0].equalsIgnoreCase("press")) {
			if (!(arg3.length == 2)) {
				p.sendMessage("§c/yt press [模板名称] §a-印刷一个现有的模板");
			}
			ModelInfo m = this.getModel(arg3[1], p);
			if (m == null) {
				p.sendMessage(Main.getMessageManager().getMessage_UnknownModel());
				return true;
			}
			Main.getPressManager().openInventory(p, m);
			return true;
		}
		return true;
	}

	private ModelInfo getModel(String string, Player p) {
		PlayerData pa = Main.getPlayerDataChenter().getPlayerData(p);
		return pa.readModeInfo(string);

	}

}
